﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BestLifePOS_Core.Data.Fascade;
using BestLifePOS_Core.DomainObjects;

namespace BestLifePOS_PresentationLayer
{
    public partial class frm_Best_Life : Form
    {
        #region Globel variables
        DataTable GvAlldetailsPos = new DataTable();
        DataTable GvUserDetails = new DataTable();
        DataTable Gvpopupdetails = new DataTable();

        List<User> Gvlistuserdetails = new List<User>();

        string Gvdis_name = "";
        int Gvflagid = 0;
        string Gvterminalid="";
        string Gvbranchname = "";
 

        #endregion

        public frm_Best_Life(string Pdis_name, int Pflag, List<User> Plistuserdetails, string Pterminalid, string Pbranchname)
        {
            InitializeComponent();
            
            #region Assign Values To Gvs
            Gvdis_name = Pdis_name;
            Gvflagid = Pflag;
            Gvlistuserdetails = Plistuserdetails;
            Gvterminalid = Pterminalid;
            Gvbranchname = Pbranchname;
            #endregion

        }

        private void txtProductid_TextChanged(object sender, EventArgs e)
        {
            tmrwaittillfullproduct.Start();
        }

            #region GridFill
        public void GridFill()
        {
               DataTable GvUserDetailsGlobel = new DataTable();
               #region GVUserDetailsGlobel--Datatable

               GvUserDetailsGlobel.Columns.Add("Pro_ID");
               GvUserDetailsGlobel.Columns.Add("Pl_Id");
               GvUserDetailsGlobel.Columns.Add("Brand_Name");
               GvUserDetailsGlobel.Columns.Add("Pro_Name");
               GvUserDetailsGlobel.Columns.Add("Pro_Price");
               GvUserDetailsGlobel.Columns.Add("Ret_Price");
               GvUserDetailsGlobel.Columns.Add("PV");
               GvUserDetailsGlobel.Columns.Add("BV");
               GvUserDetailsGlobel.Columns.Add("Quantity");


               #endregion
                
                GvUserDetails = ProductDAO.GetProductByIdForPos(txtProductid.Text, "0");
                //if (GvUserDetails.Rows.ToString().Equals(null))
                //{

                if (GvUserDetails.Rows.Count>0) 
                {

                for (int i = 0; i <= GvUserDetails.Rows.Count - 1; i++)
                {
                    GvUserDetailsGlobel.Rows.Add(GvUserDetailsGlobel.NewRow());
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][0] = GvUserDetails.Rows[i][0].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][1] = GvUserDetails.Rows[i][1].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][2] = GvUserDetails.Rows[i][2].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][3] = GvUserDetails.Rows[i][3].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][4] = GvUserDetails.Rows[i][4].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][5] = GvUserDetails.Rows[i][5].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][6] = GvUserDetails.Rows[i][6].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][7] = GvUserDetails.Rows[i][7].ToString();
                    GvUserDetailsGlobel.Rows[GvUserDetailsGlobel.Rows.Count - 1][8] = GvUserDetails.Rows[i][8].ToString();
                }

                GvAlldetailsPos.Merge(GvUserDetailsGlobel);
                gdvselectedproduct.DataSource = GvAlldetailsPos;
               
                int Lvgridrow = gdvselectedproduct.RowCount;

                
                if (Lvgridrow == 0)
                {
                    gdvselectedproduct.CurrentCell = gdvselectedproduct.Rows[Lvgridrow].Cells[8];
                }
                else 
                {
                    gdvselectedproduct.CurrentCell = gdvselectedproduct.Rows[Lvgridrow-2].Cells[8];
                }

                gdvselectedproduct.BeginEdit(true);
                tmrproductQtytime.Start();
                }
                else
                {
                    //MessageBox.Show("No pOduct");
                }

        }
        #endregion

        private void frm_Best_Life_Load(object sender, EventArgs e)
        {
            #region GvAlldetailsPos--Datatable

            GvAlldetailsPos.Columns.Add("Pro_ID");
            GvAlldetailsPos.Columns.Add("Pl_Id");
            GvAlldetailsPos.Columns.Add("Brand_Name");
            GvAlldetailsPos.Columns.Add("Pro_Name");
            GvAlldetailsPos.Columns.Add("Pro_Price");
            GvAlldetailsPos.Columns.Add("Ret_Price");
            GvAlldetailsPos.Columns.Add("PV");
            GvAlldetailsPos.Columns.Add("BV");
            GvAlldetailsPos.Columns.Add("Quantity");


            #endregion

            #region Set Distributor And System User info
            lblloginuser.Text = Gvlistuserdetails[0].Use_Name.ToString();
            lblcustomerid.Text = Gvdis_name;
            #endregion
        }

       

        //private void gdvselectedproduct_CellEnter(object sender, DataGridViewCellEventArgs e)
        //{

        //    if ((7 == e.ColumnIndex))
        //    {
        //        MessageBox.Show("Show :)");
        //    }
        //}

        private void gdvselectedproduct_KeyPress(object sender, KeyPressEventArgs e)
        {
            
            if (e.KeyChar == (char)Keys.Space)
            {
                tmrproductQtytime.Stop(); 
                txtProductid.Text = "";
                
            }

        }

        private void productQtytime_Tick(object sender, EventArgs e)
        {
            gdvselectedproduct.EndEdit();
            Setvalues_forst();
  
        }

        private void waittillfullproduct_Tick(object sender, EventArgs e)
        {
            #region Select Detais From ProductID
            GvUserDetails = ProductDAO.GetProductByIdForPos(txtProductid.Text, "0");
            if (GvUserDetails.Rows.Count > 1)
            {
                tmrwaittillfullproduct.Stop();
                frmPooupbl Lvfrmpop = new frmPooupbl(txtProductid.Text);
                Lvfrmpop.IdentityUpdated += new frmPooupbl.IdentityUpdateHandler(pooupbl_passed);
                Lvfrmpop.ShowDialog();
            }
            else
            {
                tmrwaittillfullproduct.Stop();
                GridFill();

                Setvalues_forst();
            }
            #endregion
        }


        private void pooupbl_passed(object sender, IdentityUpdateEventArgs e)
        {
            // update the forms values from the event args
            DataTable LvPUserDetailsGlobel = new DataTable();
            DataSet ds = new DataSet();
            LvPUserDetailsGlobel = e.Passtable;
            GvAlldetailsPos.Merge(LvPUserDetailsGlobel);

           gdvselectedproduct.DataSource = GvAlldetailsPos;
           int Lvgridrow = gdvselectedproduct.RowCount;

                
                if (Lvgridrow == 0)
                {
                    gdvselectedproduct.CurrentCell = gdvselectedproduct.Rows[Lvgridrow].Cells[8];
                }
                else 
                {
                    gdvselectedproduct.CurrentCell = gdvselectedproduct.Rows[Lvgridrow-2].Cells[8];
                }

                gdvselectedproduct.BeginEdit(true);
                tmrproductQtytime.Start();
                Setvalues_forst();

        }

        public void Setvalues_forst()
        {
            #region Value Varibles
            

            decimal Lvprice = 0.00m;
            decimal LvretailPrice = 0.00m;
            decimal LvPV =0.00m;
            decimal LvBV = 0.00m;
            decimal Qty = 0.00m;


            #endregion

            foreach (DataGridViewRow rows in gdvselectedproduct.Rows) 
            {
                if (rows.Cells["Pro_ID"].Value != null && rows.Cells["Quantity"].Value !=null)
                {
                    Qty = decimal.Round(decimal.Parse(rows.Cells["Quantity"].Value.ToString()), 3);
                    Lvprice += (decimal.Round(decimal.Parse(rows.Cells["Pro_Price"].Value.ToString()))*Qty);
                    LvretailPrice += (decimal.Round(decimal.Parse(rows.Cells["Ret_Price"].Value.ToString())) * Qty);
                    LvPV += (decimal.Round(decimal.Parse(rows.Cells["PV"].Value.ToString()), 3) * Qty);
                    LvBV += (decimal.Round(decimal.Parse(rows.Cells["BV"].Value.ToString()), 3) * Qty);
                }
                else { }

                if (lblcustomerid.Text.Equals("New Customer"))
                {
                    lbltotalprice.Text = LvretailPrice.ToString();
                }
                else 
                { 
                    lbltotalprice.Text = Lvprice.ToString(); 
                }
                lblPVs.Text = LvPV.ToString();
                lblBVs.Text = LvBV.ToString();
            }

            
        }

        private void btnPaybill_Click(object sender, EventArgs e)
        {
            if (rbtCreditcard.Checked || rbtnCash.Checked || rbtvoucher.Checked)
            {

                if (MessageBox.Show("Will you really Add the item?", "Warning", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {

                    BindingSource bs = new BindingSource();
                    bs.DataSource = gdvselectedproduct.DataSource;
                    DataTable newdt = new DataTable();

                    newdt = (DataTable)bs.DataSource;


                    DataSet LvTotables = new DataSet("rcptdetails");
                    LvTotables.Tables.Add(newdt.Copy());

                    string xmlrcptstring = LvTotables.GetXml();
                    string Lvreturn = SaveRcpt(xmlrcptstring);

                    if (Lvreturn != "")
                    {

                        MessageBox.Show("Success");
                        frmReportViewer frmR = new frmReportViewer(4, Lvreturn);
                        frmR.ShowDialog();
                    }

                }
                else
                {


                }
            }
            else { MessageBox.Show("Pls select Payment methord !!"); }
        }
        

        private void button8_Click(object sender, EventArgs e)
        {
            //hi i m piumi
        }

            //foreach (DataGridViewRow row in gdvselectedproduct.Rows) 
            //{
            


            //}

            


        private string SaveRcpt(string xmlstring)
        {
            string Lvxml = xmlstring;

            Recipt_Detail LvRcpt = collectRcpttbl();
            if (LvRcpt != null)
            {
                return Recipt_DetailDAO.SaveRecipt(LvRcpt, Lvxml);
            }
            else
            {
              return "";
            }
        }

        private Recipt_Detail collectRcpttbl() 
        {
            Recipt_Detail rcptdetails = new Recipt_Detail();
            rcptdetails.Dis_ID = lblcustomerid.Text;
            rcptdetails.Total_Price = decimal.Parse(lbltotalprice.Text.ToString());
            rcptdetails.Branch_Id = Gvbranchname;
            rcptdetails.Status = "";
            rcptdetails.Total_PV = decimal.Parse(lblPVs.Text.ToString());
            rcptdetails.Total_BV = decimal.Parse(lblBVs.Text.ToString());

            if(rbtnCash.Checked)
            {
                rcptdetails.Pay_Method = "Cash";
            }
            else if (rbtvoucher.Checked)
            {
                rcptdetails.Pay_Method = "Voucher";
            }
            else if (rbtCreditcard.Checked)
            {
                rcptdetails.Pay_Method = "Credit_Card";
            }

            rcptdetails.User_Id = lblloginuser.Text;
            rcptdetails.Date_Create = System.DateTime.Now;
            rcptdetails.Last_Modify = System.DateTime.Now;
            rcptdetails.Term_Id = Gvterminalid;


            return rcptdetails;
        }

        private void gdvselectedproduct_KeyPress_1(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Space)
            {
                tmrproductQtytime.Stop();
                txtProductid.Text = "";

            }

        }

        private void btnAdmin_Click(object sender, EventArgs e)
        {
            frm_Main main = new frm_Main();
            main.ShowDialog();
        }

        
       
    }
}
